// pages/blog/blog.js
const app = getApp();
const db = wx.cloud.database();
const blog = require('../../core/parseBlog');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content:'',
    vis:true,
    favor:false,
    postData:null,
    modalName:'',
    items: [
      { name: '未分类'},
      { name: '小程序'}
    ],
    type:'未分类',
    newType:'',
    favorid:''
  },
  gettype(e){
    this.setData({
      newType:e.detail.value
    })
  },
  addtype(){
    wx.showLoading({
      title: '新增分类',

    })
    db.collection('TYPE_LIST').where({
      userid:app.globalData.openid,
      name:this.data.newType
    }).get().then(ans=>{
      if(ans.data.length>0 || this.data.newType=='小程序' || this.data.newType=='未分类'){
        wx.showToast({
          title: '该分类已存在',
          icon:'none'
        })
      }else{
        db.collection('TYPE_LIST').add({
          data:{
            userid:app.globalData.openid,
            name:this.data.newType
          },
          success:res=>{
            console.log(res)
            this.setData({
              items:this.data.items.concat({
                name:this.data.newType
              }),
              newType:''
            })
            wx.hideLoading({
              success: (res) => {},
            })
          }
        })
      }
    })
    
  },
  radioChange(e) {
    console.log(e)
    console.log('radio发生change事件，携带value值为：', e.currentTarget.dataset.sel)
    this.setData({
      type:e.currentTarget.dataset.sel,
      vis:true
    },()=>{
      let type = this.data.type
      let userid = app.globalData.openid
      let postData = this.data.postData;
      console.log(postData)
      delete postData.htmldata
      postData.postid = postData._id
      delete postData._id
      postData.userid = userid
      postData.type = type
      delete postData._openid
      db.collection('FAVOR_LIST').add({
        data:postData
      }).then(res=>{
        wx.showToast({
          title: '收藏成功',
          icon:'none'
        })
        this.setData({
          vis:false,
          favor:true,
          modalName:''
        })
      })
    })
  },
  recfavor(){
    let postid = this.data.postData._id;
    let userid = app.globalData.userid;
    db.collection('FAVOR_LIST').doc(this.data.favorid).remove().then(res=>{
      console.log(res)
      wx.showToast({
        title: '取消收藏成功',
        icon:'none'
      })
      this.setData({
        favor:false
      })
    })
  },
  favor(){
    let userid = app.globalData.openid;//用户身份ID
    this.setData({
      modalName:'modalshow'
    })
  },
  wxmlTagATap(e){
    //点击地址
   // console.log(e)
    let url = e.detail.src;
    console.log(url)
    this.setData({
      vis:true,
      taglist:[]
    })
    blog.parseBlog(url).then(res => {
      console.log(res)
      if (res == undefined) {
        console.log('非可解析文章')
        this.setData({
          vis:false
        })
      } else {
        //this.open();
        wx.setNavigationBarTitle({
          title: res.result.title,
        })
        this.setData({
          postData:res.result,
          articleinfo: res.result.articledata,
          content:res.result.htmldata,
        })
        this.setData({
          vis:false
        })
        
      }

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取当前文章信息
    db.collection('POST_LIST').doc(options.id).get().then(res=>{
      console.log(res)
      this.setData({
        content:res.data.htmldata,
        articleinfo:res.data.articleinfo,
        vis:false,
        taglist:res.data.taglist,
        postData:res.data
      })
      wx.setNavigationBarTitle({
        title: res.data.title,
      })
    })
    //获取当前用户自定义分类信息
    db.collection('TYPE_LIST').where({
      userid:app.globalData.openid
    }).get().then(res=>{
      console.log(res)
      this.setData({
        items:this.data.items.concat(res.data)
      })
    })
    //获取是否收藏了该篇文章
    let userid = app.globalData.openid;
    let postid = options.id
    db.collection('FAVOR_LIST').where({
      userid,
      postid
    }).get().then(res=>{
      console.log(res)
      if(res.data.length>0){
        this.setData({
          favor:true,
          favorid:res.data[0]._id
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})